<template>
    <view>
        <block v-if="isAdmin && isLoad">
            <cmpt-comm-list
                source="admin"
                :pulldownType="['modal', 'modal', 'modal']"
                type="admin-meet"
                :search="search || ''"
                :_menus="sortMenus"
                :_items="sortItems"
                route="admin/meetList"
                sortMenusDefaultIndex="0"
                topBottom="50"
                placeholder="搜索标题"
                @list="bindCommListCmpt"
            >
                <view slot="searchEnd">
                    <button @tap="url" data-url="/pages/admin/meet/add/admin_meet_add" class="btn radius bg-admin text-white margin-right-s">
                        <text class="icon-roundadd margin-right-xxs"></text>
                        创建老师&预约
                    </button>
                </view>

                <!-- List Begin -->
                <view class="admin-comm-list">
                    <view v-if="dataList && dataList.total" class="load text-grey">共有{{ dataList.total }}条符合条件记录</view>

                    <view class="item" v-for="(item, index) in dataList.list" :key="index">
                        <view class="no">{{ index + 1 }}</view>

                        <view class="header">
                            <view class="left text-cut">
                                <text v-if="item.isTop == 1" class="text-black" style="font-weight: normal; font-size: 24rpx">[置顶]</text>
                                <text v-if="item.isVouch == 1" class="text-black margin-right-xxs" style="font-weight: normal; font-size: 24rpx">[首页推荐]</text>
                                {{ item.teacherName }}
                            </view>
                            <view class="right" @tap="bindScanTap" :data-id="item.id" :data-title="item.teacherName">
                                <text class="icon-scan"></text>
                            </view>
                        </view>

                        <view class="info">
                            <view class="info-item">
                                <view class="title">状态</view>
                                <view class="mao">：</view>
                                <view class="content">
                                    <text v-if="item.status == 0" class="text-grey">未启用</text>
                                    <text v-else-if="item.status == 1" class="text-green">使用中</text>
                                    <text v-else-if="item.status == 9" class="text-orange">停止 (用户可见)</text>
                                    <text v-else-if="item.status == 10" class="text-red">已关闭 (用户不可见)</text>
                                </view>
                            </view>

                            <view v-if="cateIdOptions.length > 1" class="info-item">
                                <view class="title">分类</view>
                                <view class="mao">：</view>
                                <view class="content">【{{ item.courseName }}】</view>
                            </view>

                            <view class="info-item">
                                <view class="title">排序号</view>
                                <view class="mao">：</view>
                                <view class="content">
                                    {{ item.sort }}
                                    <text class="margin-left-xxs text-grey">(小的先显示)</text>
                                </view>
                            </view>

                            <view class="info-item">
                                <view class="title">排期</view>
                                <view class="mao">：</view>
                                <view :class="'content ' + (item.leaveDay == 0 ? 'text-grey' : '')">{{ item.leaveDay }}天可预约</view>
                            </view>

                            <view class="info-item">
                                <view class="title">修改</view>
                                <view class="mao">：</view>
                                <view class="content">{{ item.updatedTime }}</view>
                            </view>

                            <view class="oprt">
                                <view @tap="url" :data-url="'../edit/admin_meet_edit?id=' + item.id" class="btn margin-right-s">
                                    <text class="icon-settings margin-right-xxs text-blue"></text>
                                    设置
                                </view>

                                <view @tap="bindRecordSelectTap" :data-id="item.id" :data-teacherId="item.teacherId" :data-title="item.teacherName" class="btn margin-right-s">名单与核销</view>

                                <view @tap="bindStatusSelectTap" :data-id="item.id" :data-teacherId="item.teacherId" class="btn margin-right-s">
                                    <text class="icon-cascades margin-right-xxs text-orange"></text>
                                    状态
                                </view>

                                <view @tap="bindMoreSelectTap" :data-id="item.id" :data-teacherId="item.teacherId" :data-idx="index" :data-qr="item.qrCode" :data-title="item.teacherName" class="btn">更多..</view>
                            </view>
                        </view>
                    </view>
                </view>
                <!-- List END -->

                <!-- load begin -->

                <!-- parse <template is="listLoadTpl" :data="skin:'text-grey',dataList"/> -->
                <block name="listLoadTpl" v-if="false">
                    <view v-if="!dataList || (dataList && dataList.page < dataList.count)" :class="'load text-grey loading ' + 'text-grey'"></view>
                    <view v-if="dataList && dataList.page > 1 && dataList.page == dataList.count" :class="'load text-grey over ' + 'text-grey'"></view>

                    <block v-if="noHintImg">
                        <image style="height: 500rpx" mode="aspectFit" v-if="dataList && dataList.total == 0" :src="noHintImg" />
                    </block>

                    <block v-else>
                        <view v-if="dataList && dataList.total == 0" :class="'load text-grey ' + 'text-grey'">{{ '暂无记录' ? '暂无记录' : '暂无记录哦~' }}</view>
                    </block>

                    <view style="height: 200rpx"></view>
                </block>
                <!-- load end -->
            </cmpt-comm-list>
        </block>
    </view>
</template>

<script>
import AdminBiz from '@/utils/comm/biz/admin_biz.js';
import MeetBiz from '@/utils/biz/meet_biz.js';
import AdminMeetBiz from '@/utils/biz/admin_meet_biz.js';
import pageHelper from '@/utils/helper/page_helper.js';
import cloudHelper from '@/utils/helper/cloud_helper.js';
import projectSetting from '@/utils/public/project_setting.js';
const api = require('@/api/adminapi.js');

export default {
    data() {
        return {
            MEET_NAME: '',

            dataList: {
                total: '',
                list: [],
                page: 0,
                count: 0
            },

            search: '',
            cateIdOptions: '',
            sortItems: '',
            sortMenus: '',
            isLoad: false,
            isAdmin: '',
            noHintImg: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: async function (options) {
        if (!AdminBiz.isAdmin(this)) {
            return;
        }
        uni.setNavigationBarTitle({
            title: projectSetting.MEET_NAME + '-管理'
        });
        this.setData({
            MEET_NAME: projectSetting.MEET_NAME
        });

        //设置搜索菜单
        await this.getSearchMenuFun();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: async function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    methods: {
        url: async function (e) {
            pageHelper.url(e, this);
        },

        bindCommListCmpt: function (e) {
            pageHelper.commListListener(this, e);
        },

        bindScanTap: function (e) {
            let meetId = pageHelper.dataset(e, 'id');
            let title = encodeURIComponent(pageHelper.dataset(e, 'title'));
            uni.navigateTo({
                url: '../scan/admin_meet_scan?meetId=' + meetId + '&title=' + title
            });
        },

        bindRecordSelectTap: async function (e) {
            let itemList = ['预约名单', '导出名单Excel文件', '管理员核销预约码'];
            let meetId = pageHelper.dataset(e, 'id');
            let title = encodeURIComponent(pageHelper.dataset(e, 'title'));
            uni.showActionSheet({
                itemList,
                success: async (res) => {
                    switch (res.tapIndex) {
                        case 0: {
                            //预约名单
                            uni.navigateTo({
                                url: '../record/admin_meet_record?meetId=' + meetId + '&title=' + title
                            });
                            break;
                        }
                        case 1: {
                            //导出
                            uni.navigateTo({
                                url: '../export/admin_join_export?meetId=' + meetId + '&title=' + title
                            });
                            break;
                        }
                        case 2: {
                            //核验
                            this.bindScanTap(e);
                            break;
                        }
                    }
                },
                fail: function (res) {}
            });
        },

        setSortFun: async function (e) {
            if (!AdminBiz.isAdmin(this)) {
                return;
            }
            let meetId = pageHelper.dataset(e, 'id');
            let isTop = pageHelper.dataset(e, 'isTop');
            let idx = pageHelper.dataset(e, 'idx');
            console.log('idx', idx);
            if (!meetId) {
                return;
            }
            let params = {
                id:meetId,
                isTop,
                type: 2
            };
            try {
                api.setTopItem(params).then((res) => {
                    this.dataList.list[idx].isTop = isTop;
                    this.setData({
                        dataList: this.dataList
                    });
                    pageHelper.showSuccToast('设置成功');
                });
            } catch (e) {
                console.log('CatchClause', e);
                console.log('CatchClause', e);
                console.log(e);
            }
        },

        setVouchFun: async function (e) {
            if (!AdminBiz.isAdmin(this)) {
                return;
            }
            let id = pageHelper.dataset(e, 'id');
            let vouch = pageHelper.dataset(e, 'vouch');
            let idx = pageHelper.dataset(e, 'idx');
            console.log('idx', idx);
            if (!id) {
                return;
            }
            let params = {
                id,
                vouch,
                type: 2
            };
            try {
                api.vTouchItem(params).then((res) => {
                    this.dataList.list[idx].vtouch = vouch;
                    this.setData({
                        dataList: this.dataList
                    });
                    pageHelper.showSuccToast('设置成功');
                });
            } catch (err) {
                console.log('CatchClause', err);
                console.log('CatchClause', err);
                console.log(err);
            }
        },

        bindMoreSelectTap: async function (e) {
            if (!AdminBiz.isAdmin(this)) {
                return;
            }
            let idx = pageHelper.dataset(e, 'idx');
            let order = this.dataList.list[idx].isTop;
            //order 可以为空
            let orderDesc = order == 1 ? '取消置顶' : '置顶';
            let vouch = this.dataList.list[idx].vtouch;
            let vouchDesc = vouch == 0 ? '推荐到首页' : '取消首页推荐';
            let itemList = ['预览', orderDesc, vouchDesc, '生成专属二维码'];
            uni.showActionSheet({
                itemList,
                success: async (res) => {
                    switch (res.tapIndex) {
                        case 0: {
                            //预览
                            let meetId = pageHelper.dataset(e, 'id');
                            uni.navigateTo({
                                url: pageHelper.fmtURLByPID('/pages/meet/detail/meet_detail?id=' + meetId)
                            });
                            break;
                        }
                        case 1: {
                            //置顶
                            let newIsTop = order == 0 ? 1 : 0;
                            e.currentTarget.dataset['isTop'] = newIsTop;
                            await this.setSortFun(e);
                            break;
                        }
                        case 2: {
                            //上首页
                            vouch = vouch == 0 ? 1 : 0;
                            e.currentTarget.dataset['vouch'] = vouch;
                            await this.setVouchFun(e);
                            break;
                        }
                        case 3: {
                            //二维码
                            let title = encodeURIComponent(pageHelper.dataset(e, 'title'));
                            let qr = encodeURIComponent(pageHelper.dataset(e, 'qr'));
                            uni.navigateTo({
                                url: `../../setup/qr/admin_setup_qr?title=${title}&qr=${qr}`
                            });
                            break;
                        }
                    }
                },
                fail: function (res) {}
            });
        },

        bindStatusSelectTap: async function (e) {
            let itemList = ['启用', '停止预约 (用户可见)', '关闭 (用户不可见)', '删除'];
            let meetId = pageHelper.dataset(e, 'id');
            uni.showActionSheet({
                itemList,
                success: async (res) => {
                    switch (res.tapIndex) {
                        case 0: {
                            //启用
                            await this.setStatusFun(this, meetId, 1);
                            break;
                        }
                        case 1: {
                            //停止预约
                            await this.setStatusFun(this, meetId, 9);
                            break;
                        }
                        case 2: {
                            //关闭
                            await this.setStatusFun(this, meetId, 10);
                            break;
                        }
                        case 3: {
                            //删除
                            await this.delFun(this, meetId);
                            break;
                        }
                    }
                },
                fail: function (res) {}
            });
        },

        delFun: async function (that, meetId) {
            if (!AdminBiz.isAdmin(this)) {
                return;
            }
            if (!meetId) {
                return;
            }
            let params = {
                id:meetId
            };
            let callback = async function () {
                try {
                    let opts = {
                        title: '删除中'
                    };
                    await api.delTeacher(params, opts).then((res) => {
                        pageHelper.delListNode(meetId, that.dataList.list, 'id');
                        that.data.dataList.total--;
                        that.setData({
                            dataList: that.data.dataList
                        });
                        pageHelper.showSuccToast('删除成功');
                    });
                } catch (e) {
                    console.log('CatchClause', e);
                    console.log('CatchClause', e);
                    console.log(e);
                }
            };
            pageHelper.showConfirm('确认删除？删除不可恢复', callback);
        },

        setStatusFun: async function (that, meetId, status) {
            if (!AdminBiz.isAdmin(this)) {
                return;
            }
            if (!meetId) {
                return;
            }
            let params = {
                id: meetId,
                status
            };
            api.updateTeacherStatus(params).then(res => {
                pageHelper.modifyListNode(meetId, that.dataList.list, 'status', status, 'id');
                that.setData({
                    dataList: that.dataList
                });
                pageHelper.showSuccToast('设置成功');
            });
        },

        getSearchMenuFun: async function () {
            console.log('getSearchMenuFun');
            await MeetBiz.initCateList();
            let cateIdOptions = MeetBiz.getCateList();
            let sortItem1 = [
                {
                    label: '分类',
                    type: '',
                    value: ''
                }
            ];
            sortItem1 = sortItem1.concat(MeetBiz.getCateList());
            let sortItems = [sortItem1];
            if (sortItem1.length <= 2) {
                sortItems = [];
            }
            let sortMenus = [
                {
                    label: '全部',
                    type: '',
                    value: ''
                },
                {
                    label: '使用中',
                    type: 'status',
                    value: 1
                },
                {
                    label: '已停止',
                    type: 'status',
                    value: 9
                },
                {
                    label: '已关闭',
                    type: 'status',
                    value: 10
                },
                {
                    label: '首页推荐',
                    type: 'vouch',
                    value: 'vouch'
                },
                {
                    label: '置顶',
                    type: 'top',
                    value: 'top'
                }
            ];
            this.setData({
                search: '',
                cateIdOptions,
                sortItems,
                sortMenus,
                isLoad: true
            });
        }
    }
};
</script>
<style>
@import './admin_meet_list.css';
</style>
